﻿// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System;

namespace IdentityModel
{
  /// <summary>Base64Url encoder/decoder</summary>
  public static class Base64Url
  {
    /// <summary>Encodes the specified byte array.</summary>
    /// <param name="arg">The argument.</param>
    /// <returns></returns>
    public static string Encode(byte[] arg)
    {
      var s = Convert.ToBase64String(arg); // Standard base64 encoder

      s = s.Split('=')[0]; // Remove any trailing '='s
      s = s.Replace('+', '-'); // 62nd char of encoding
      s = s.Replace('/', '_'); // 63rd char of encoding

      return s;
    }

    /// <summary>Decodes the specified string.</summary>
    /// <param name="arg">The argument.</param>
    /// <returns></returns>
    /// <exception cref="System.Exception">Illegal base64url string!</exception>
    public static byte[] Decode(string arg)
    {
      var s = arg;
      s = s.Replace('-', '+'); // 62nd char of encoding
      s = s.Replace('_', '/'); // 63rd char of encoding

      switch (s.Length % 4) // Pad with trailing '='s
      {
        case 0: break; // No pad chars in this case
        case 2: s += "=="; break; // Two pad chars
        case 3: s += "="; break; // One pad char
        default: throw new Exception("Illegal base64url string!");
      }

      return Convert.FromBase64String(s); // Standard base64 decoder
    }
  }
}